package org.topic.surpermarket;

import java.math.BigDecimal;

/**
 * 商品抽象基类
 */
public abstract class Product {

    private String name;      // 商品名称

    private BigDecimal price; // 商品价格

    /**
     * 商品构造参数
     * @param name
     * @param price
     */
    public Product(String name, BigDecimal price) {
        if (price.compareTo(BigDecimal.ZERO) <= 0) {
            throw new IllegalArgumentException("商品价格必须大于0");
        }
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    /**
     * 计算商品总价
     * @param quantity 商品数量
     * @return 总价
     */
    public BigDecimal calculatePrice(int quantity) {
        if (quantity < 0) {
            throw new IllegalArgumentException("商品数量不能为负数");
        }
        return price.multiply(BigDecimal.valueOf(quantity));
    }
}